package org.csu.backofficemanagersys.controller;

import com.alibaba.fastjson.JSONObject;
import org.csu.backofficemanagersys.Response.ResponseMsg;
import org.csu.backofficemanagersys.domain.Category;
import org.csu.backofficemanagersys.domain.Item;
import org.csu.backofficemanagersys.domain.Product;
import org.csu.backofficemanagersys.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/mypetstore")
public class ItemController {
    private ItemService itemService;
    @Autowired
    private void setItemService(ItemService itemService){this.itemService = itemService;}

    @GetMapping("/category/index")
    public void Category(){
    }

    @GetMapping("/category/table")
    public ResponseMsg<JSONObject> findC(int currentPage, int size,String catid){
        if(catid == null){
            return SelectOneByName(currentPage,size);
        }
        try {
            List<Category> categoryList = itemService.findCategoryLike(catid);
            int total = categoryList.size();
            if(total == 0){
                return ResponseMsg.createForSuccess("表格为空",null);
            }
            JSONObject jsonObject = new JSONObject();
            List<Category> categories;
            if(total<size*currentPage && total>(currentPage-1)*size){
                categories = categoryList.subList(size * (currentPage - 1), total);
            }else {
                categories = categoryList.subList(size * (currentPage - 1), size * currentPage - 1);
            }
            jsonObject.put("total",total);
            jsonObject.put("list",categories);
            return ResponseMsg.createForSuccess("获取表格元素成功",jsonObject);
        }catch (Exception e){
            return ResponseMsg.createForError("获取表格元素失败");
        }
    }
    @PutMapping("/category/table")
    public ResponseMsg<Objects> UpdateCategory(@ModelAttribute Category category){
        if(itemService.updateCategory(category)){
            return ResponseMsg.createForSuccess();
        }
        return ResponseMsg.createForError();
    }
    @DeleteMapping("/category/table/{catid}")
    public ResponseMsg<Objects> DeleteCategory(@PathVariable String catid){
        if(itemService.deleteCategory(catid)){
            return ResponseMsg.createForSuccess();
        }
        return ResponseMsg.createForError();
    }
    @PostMapping("/category/table")
    public ResponseMsg<Objects> InsertCategory(@ModelAttribute Category category){
        if(itemService.insertCategory(category)){
            return ResponseMsg.createForSuccess();
        }
        return ResponseMsg.createForError();
    }
    @GetMapping("/category/exist/{catid}")
    public ResponseMsg<Objects> CisExit(@PathVariable String catid){
        if(itemService.getCategoryByName(catid) != null){
            return ResponseMsg.createForSuccessMsg("category已存在");
        }
        return ResponseMsg.createForSuccessMsg("category不存在");
    }

    @GetMapping("/product/index")
    private void Product(){

    }

    @GetMapping("/product/table")
    public ResponseMsg<JSONObject> findP(int currentPage, int size,String productid){
        if(productid == null){
            return TableP(currentPage,size);
        }
        try {
            List<Product> productList = itemService.findProductLike(productid);
            int total = productList.size();
            if(total == 0){
                return ResponseMsg.createForSuccess("表格为空",null);
            }
            JSONObject jsonObject = new JSONObject();
            List<Product> categories;
            if(total<size*currentPage && total>(currentPage-1)*size){
                categories = productList.subList(size * (currentPage - 1), total);
            }else {
                categories = productList.subList(size * (currentPage - 1), size * currentPage - 1);
            }
            jsonObject.put("total",total);
            jsonObject.put("list",categories);
            return ResponseMsg.createForSuccess("获取表格元素成功",jsonObject);
        }catch (Exception e){
            return ResponseMsg.createForError("获取表格元素失败");
        }
    }
    @PutMapping("/product/table")
    public ResponseMsg<Objects> UpdateProduct(@ModelAttribute Product product){
        if(itemService.updateProduct(product)){
            return ResponseMsg.createForSuccess();
        }
        return ResponseMsg.createForError();
    }
    @DeleteMapping("/product/table/{productid}")
    public ResponseMsg<Objects> DeleteProduct(@PathVariable String productid){
        if(itemService.deleteProduct(productid)){
            return ResponseMsg.createForSuccess();
        }
        return ResponseMsg.createForError();
    }
    @PostMapping("/product/table")
    public ResponseMsg<Objects> InsertProduct(@ModelAttribute Product product){
        if(itemService.insertProduct(product)){
            return ResponseMsg.createForSuccess();
        }
        return ResponseMsg.createForError();
    }
    @GetMapping("/product/exist/{productid}")
    public ResponseMsg<Objects> PisExit(@PathVariable String productid){
        if(itemService.getProductById(productid)!=null){
            return ResponseMsg.createForSuccessMsg("product已存在");
        }
        return ResponseMsg.createForSuccessMsg("product不存在");
    }

    @GetMapping("/item/index")
    private ResponseMsg<List<Item>> Item(){
        List<Item> items = itemService.getAllItem();
        return ResponseMsg.createForSuccess(items);
    }

    @GetMapping("/item/table")
    public ResponseMsg<JSONObject> findI(int currentPage, int size,String itemid){
        if(itemid == null){
            return TableI(currentPage,size);
        }
        try {
            List<Item> productList = itemService.findItemLike(itemid);
            if(productList.size() == 0){
                return ResponseMsg.createForSuccess("表格为空",null);
            }
            int total = productList.size();
            JSONObject jsonObject = new JSONObject();
            List<Item> categories;
            if(total<size*currentPage && total>(currentPage-1)*size){
                categories = productList.subList(size * (currentPage - 1), total);
            }else {
                categories = productList.subList(size * (currentPage - 1), size * currentPage - 1);
            }
            jsonObject.put("total",total);
            jsonObject.put("list",categories);
            return ResponseMsg.createForSuccess("获取表格元素成功",jsonObject);
        }catch (Exception e){
            return ResponseMsg.createForError("获取表格元素失败");
        }
    }
    @PutMapping("/item/table")
    public ResponseMsg<Objects> UpdateItem(@ModelAttribute Item item){
        if(itemService.updateItem(item)){
            return ResponseMsg.createForSuccess();
        }
        return ResponseMsg.createForError();
    }
    @DeleteMapping("/item/table/{itemid}")
    public ResponseMsg<Objects> DeleteItem(@PathVariable String itemid){
        if(itemService.deleteItem(itemid)){
            return ResponseMsg.createForSuccess();
        }
        return ResponseMsg.createForError();
    }
    @PostMapping("/item/table")
    public ResponseMsg<Objects> InsertItem(@ModelAttribute Item item){
        if(itemService.insertItem(item)){
            return ResponseMsg.createForSuccess();
        }
        return ResponseMsg.createForError();
    }
    @GetMapping("/item/exist/{itemid}")
    public ResponseMsg<Objects> IisExit(@PathVariable String itemid){
        if(itemService.getItemById(itemid)!=null){
            return ResponseMsg.createForSuccessMsg("item已存在");
        }
        return ResponseMsg.createForSuccessMsg("item不存在");
    }

    public ResponseMsg<JSONObject> SelectOneByName(int currentPage, int size){
        try {
            List<Category> categoryList = itemService.getAllCategory();
            int total = categoryList.size();
            JSONObject jsonObject = new JSONObject();
            List<Category> categories;
            if(total<size*currentPage && total>(currentPage-1)*size){
                categories = categoryList.subList(size * (currentPage - 1), categoryList.size());
            }else {
                categories = categoryList.subList(size * (currentPage - 1), size * currentPage - 1);
            }
            jsonObject.put("total",total);
            jsonObject.put("list",categories);
            return ResponseMsg.createForSuccess("获取表格元素成功",jsonObject);
        }catch (Exception e){
            return ResponseMsg.createForError("获取表格元素失败");
        }
    }
    public ResponseMsg<JSONObject> TableP(int currentPage, int size){
        try {
            List<Product> productList = itemService.getAllProduct();
            JSONObject jsonObject = new JSONObject();
            List<Product> products;
            if(productList.size()<size*currentPage && productList.size()>(currentPage-1)*size){
                products = productList.subList(size * (currentPage - 1), productList.size());
            }else {
                products = productList.subList(size * (currentPage - 1), size * currentPage - 1);
            }
            jsonObject.put("total",productList.size());
            jsonObject.put("list",products);

            return ResponseMsg.createForSuccess("获取表格元素成功",jsonObject);
        }catch (Exception e){
            return ResponseMsg.createForError("获取表格元素失败");
        }
    }
    public ResponseMsg<JSONObject> TableI(int currentPage, int size){
        try {

            List<Item> itemList = itemService.getAllItem();
            JSONObject jsonObject = new JSONObject();
            List<Item> items;
            if(itemList.size()<size*currentPage && itemList.size()>(currentPage-1)*size){
                items = itemList.subList(size * (currentPage - 1), itemList.size());
            }else {
                items = itemList.subList(size * (currentPage - 1), size * currentPage - 1);
            }
            jsonObject.put("total",itemList.size());
            jsonObject.put("list",items);

            return ResponseMsg.createForSuccess("获取表格元素成功",jsonObject);
        }catch (Exception e){
            return ResponseMsg.createForError("获取表格元素失败");
        }
    }
}
